package com.henu.cee_service.controller;

import com.henu.cee_service.constants.Constant;
import com.henu.cee_service.entity.User;
import com.henu.cee_service.exception.code.BaseResponseCode;
import com.henu.cee_service.service.UserService;
import com.henu.cee_service.utils.DataResult;
import com.henu.cee_service.vo.req.LoginReqVO;
import com.henu.cee_service.vo.req.RegisterReqVO;
import com.henu.cee_service.vo.req.UserPageReqVO;
import com.henu.cee_service.vo.resq.LoginRespVO;
import com.henu.cee_service.vo.resq.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.xml.crypto.Data;

/**
 * description: UserController <br>
 * date: 2020/2/28 17:47 <br>
 * author: JSM <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("/v1")
@Api(tags = "用户模块相关接口")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/user/login")
    @ApiOperation(value = "用户登录接口")
    public DataResult<LoginRespVO> login(@RequestBody @Valid LoginReqVO vo){
        DataResult result=DataResult.success();
        result.setData(userService.login(vo));
        return result;
    }

    @PostMapping("/user/register")
    @ApiOperation(value = "注册用户接口")
    public DataResult insertUser(@RequestBody @Valid RegisterReqVO vo){
//        System.out.println(vo);
        DataResult result = DataResult.success();
        result.setData(userService.register(vo));
//        System.out.println(result);
        return result;
    }

    @GetMapping("/user/logout")
    @ApiOperation(value = "用户登出接口")
    public DataResult logout(HttpServletRequest request){
        String accessToken=request.getHeader(Constant.ACCESS_TOKEN);
        String refreshToken=request.getHeader(Constant.REFRESH_TOKEN);
        userService.logout(accessToken,refreshToken);
        return DataResult.success();
    }

    @GetMapping("/user/unLogin")
    @ApiOperation(value = "引导客户端去登录")
    public DataResult unLogin(){
        DataResult result= DataResult.getResult(BaseResponseCode.TOKEN_ERROR);
        return result;
    }

    @PostMapping("users")
    @ApiOperation(value = "分页查询用户接口")
    @RequiresPermissions("sys:user:list")
    public DataResult<PageVO<User>> pageInfo(@RequestBody UserPageReqVO vo){
        DataResult result=DataResult.success();
        result.setData(userService.pageInfo(vo));
        return result;
    }
}
